package net.arvin.mobilebanksystem.activity;

import net.arvin.mobilebanksystem.R;
import net.arvin.mobilebanksystem.fragment.LoginFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class UserInfoActivity extends FragmentActivity implements
		OnClickListener {
	private RelativeLayout titleBar;
	private RelativeLayout backLayout;
	private TextView titleTv;

	public static UserInfoActivity userinfoActivity;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_userinfo);
		init();
	}

	private void init() {
		initView();
		initContent();
		initListener();
	}

	private void initListener() {
		backLayout.setOnClickListener(this);
	}

	private void initContent() {
		switchContent(new LoginFragment());
	}

	private void initView() {
		userinfoActivity = this;
		titleBar = (RelativeLayout) findViewById(R.id.titleBar);
		backLayout = (RelativeLayout) findViewById(R.id.backLayout);
		titleTv = (TextView) findViewById(R.id.titleTv);
	}

	public void hidenTitleBar() {
		titleBar.setVisibility(View.GONE);
	}

	public void showTitleBar() {
		titleBar.setVisibility(View.VISIBLE);
	}

	public void hidenBackLayout() {
		backLayout.setVisibility(View.GONE);
	}

	public void showBackLayout() {
		backLayout.setVisibility(View.VISIBLE);
	}

	public void setTitleTv(String title) {
		titleTv.setText(title);
	}

	public void switchContent(final Fragment content) {
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.userinfo_content, content).addToBackStack("")
				.commit();
		Log.e("fragment count---->>", getSupportFragmentManager()
				.getBackStackEntryCount() + "");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.backLayout:
			isBack();
			break;
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (ev.getAction() == MotionEvent.ACTION_DOWN) {
			View v = getCurrentFocus();
			if (isShouldHideInput(v, ev)) {

				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				if (imm != null) {
					imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
				}
			}
			return super.dispatchTouchEvent(ev);
		}
		// 必不可少，否则所有的组件都不会有TouchEvent了
		if (getWindow().superDispatchTouchEvent(ev)) {
			return true;
		}
		return onTouchEvent(ev);
	}

	public boolean isShouldHideInput(View v, MotionEvent event) {
		if (v != null && (v instanceof EditText)) {
			int[] leftTop = { 0, 0 };
			// 获取输入框当前的location位置
			v.getLocationInWindow(leftTop);
			int left = leftTop[0];
			int top = leftTop[1];
			int bottom = top + v.getHeight();
			int right = left + v.getWidth();
			if (event.getX() > left && event.getX() < right
					&& event.getY() > top && event.getY() < bottom) {
				// 点击的是输入框区域，保留点击EditText的事件
				return false;
			} else {
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			isBack();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void isBack() {
		if (getSupportFragmentManager().getBackStackEntryCount() > 2) {
			getSupportFragmentManager().popBackStack();
		} else if (getSupportFragmentManager().getBackStackEntryCount() == 2) {
			getSupportFragmentManager().popBackStack();
			hidenTitleBar();
		} else {
			moveTaskToBack(false);
		}
	}
}
